package cn.hxt.user.service.impl;

import cn.hxt.basic.constant.BaseConstants;
import cn.hxt.basic.exception.BusinessException;
import cn.hxt.basic.service.impl.BaseServiceImpl;
import cn.hxt.basic.util.MD5Utils;
import cn.hxt.basic.util.StrUtils;
import cn.hxt.org.mapper.EmployeeMapper;
import cn.hxt.user.domain.Logininfo;
import cn.hxt.user.domain.User;
import cn.hxt.user.dto.UserDto;
import cn.hxt.user.mapper.LogininfoMapper;
import cn.hxt.user.mapper.UserMapper;
import cn.hxt.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 注册
     * @param userDto
     */
    @Override
    public void phoneRegister(UserDto userDto) {
        //一，校验
        //1，空校验，全盘校验
        if (StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPassword())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                ){
            throw new BusinessException("请检查表格，您有一处未填！！");
        }
        //2判断两次密码是否相同
        if (!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new BusinessException("；两次密码不一致！！！");
        }
        //3，判断是否注册
        User userPhone = userMapper.loadByPhone(userDto.getPhone());
        if (userPhone !=null){
            throw new BusinessException("用户已存在！！！！");
        }
        //二，判断验证码是否可用
        Object userT = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        //1,判断验证码是否过期
        if (userT == null){
            throw new BusinessException("验证码已失效，请重新获取！！");
        }
        //2，判断验证码是否正确
        String users = ((String)userT).split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(users)){
            throw new BusinessException("请输入正确的验证码！！！");
        }
        //三，存数据
        //userDto转user
        User user = userDto2user(userDto);
        //user转logininfo
        Logininfo logininfo = user2logininfo(user);
        // 1.保存logininfo
        logininfoMapper.save(logininfo);//返回自增id
        // 2.保存 user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }



    private User userDto2user(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //盐值
        String code = StrUtils.getComplexRandomString(32);
        //加盐密码
        String md = MD5Utils.encrypByMd5(userDto.getPassword() + code);
        user.setSalt(code);
        user.setPassword(md);
        return user;
    }

    private Logininfo user2logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //使用spring给我们提供的Util
        // 拷贝相同字段内容     拷贝user再logininfo中有的字段内容
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }


}
